package com.example.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.base.Book;
import com.example.demowebview.R;

public class BookAdapter extends ArrayAdapter<Book> {
	
	Context context;
	int layoutId;
	ArrayList<Book> listBook;
	LayoutInflater inflater;
	ViewHolder holder;
	
	public BookAdapter(Context context, int layoutId, ArrayList<Book> listData) {
		super(context, layoutId, listData);
		this.context = context;
		this.layoutId = layoutId;
		this.listBook = listData;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(layoutId, parent, false);
			holder = new  ViewHolder(convertView);
			convertView.setTag(holder);
		} 
		holder = (ViewHolder) convertView.getTag();
		holder.setBuild(position, listBook.get(position));
		return convertView;
	}
	
	
	class ViewHolder{
		private LinearLayout root;
		private ImageView thumb;
		private TextView title;
		
		public ViewHolder(View view) {
			root = (LinearLayout) view.findViewById(R.id.root);
			thumb = (ImageView) view.findViewById(R.id.thumb);
			title = (TextView) view.findViewById(R.id.title);
		}
		
		public void setBuild(int position, Book item){
			if(position%2==0)
				root.setBackgroundColor(Color.parseColor("#f1f1f1"));
			else
				root.setBackgroundColor(Color.parseColor("#30f1f1f1"));
			thumb.setImageResource(item.idImage);
			title.setText(item.title);
		}
	}
	
}
